﻿using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using Might.Infrastructure.Services;
using Microsoft.Practices.Unity;

namespace Might.Services.Logger {
    internal class MightLogger : IMightLogger {

        private struct Message {
            public object Sender;
            public string Msg;
        }

        private readonly LoggerView view = new LoggerView();
        private readonly Dictionary< string , bool > senders = new Dictionary<string,bool>();
        private readonly List< Message > messages = new List<Message>();       

        private readonly IUnityContainer container;

        public MightLogger( IUnityContainer container ) {
            view.DeleteBtn.Click += DeleteBtn_Click;
            view.ModalDlgTestBtn.Click += ModalDlgTestBtn_Click;
            this.container = container;
        }

        public LoggerView View { get { return view; } }
        
        public void Log( object sender , string msg ) {     
        
            if( !senders.ContainsKey( sender.ToString() ) ) {
                StackPanel stack = new StackPanel {Orientation = Orientation.Horizontal};

                CheckBox check = new CheckBox {IsChecked = true};
                check.Checked += check_Changed;
                check.Unchecked += check_Changed;
                stack.Children.Add( check );
                
                TextBlock txt = new TextBlock {Text = sender.ToString(), Tag = sender};

                stack.Children.Add( txt );
                view.sendersComboBox.Items.Add( stack );
                
                senders.Add( sender.ToString() , true );                                
                
            }
                        
            Message message = new Message() { Msg = msg , Sender = sender };
            messages.Add( message );
            
            if( senders[ sender.ToString() ] ) {            
                addMsg( sender , msg );
            }
            
        }                               

        private void check_Changed( object sender , RoutedEventArgs e ) {            
            for( int i = 0 ; i < view.sendersComboBox.Items.Count ; i++ ) {
                object obj = ( ( view.sendersComboBox.Items[i] as StackPanel ).Children[1] as TextBlock ).Tag;
                bool? value = ( ( view.sendersComboBox.Items[i] as StackPanel ).Children[0] as CheckBox ).IsChecked;
                if( value.HasValue ) {
                    senders[ obj.ToString() ] = (bool)value;
                }                
            }
            view.MsgListBox.Items.Clear();
            for( int i = 0 ; i < messages.Count; i++ ) { 
                if( senders[ messages[i].Sender.ToString() ] ) {
                    addMsg( messages[i].Sender , messages[i].Msg );
                }
            }
        }
        
        private void addMsg( object sender , string msg ) {            
            view.MsgListBox.Items.Insert( 0 , sender + " : " + msg );            
            view.MsgListBox.SelectedIndex = 0;            
        }

        void DeleteBtn_Click( object sender , RoutedEventArgs e ) {                                    
            senders.Clear();
            messages.Clear();
            view.sendersComboBox.Items.Clear();
            view.MsgListBox.Items.Clear();                                                
        }

        void ModalDlgTestBtn_Click( object sender , RoutedEventArgs e ) {
            IModalDialog dlg = container.Resolve< IModalDialog >();
            dlg.Show( "test" , "test message " , ModalDialogButtons.OKCancel );
            dlg.Closed +=dlg_Closed;
        }        


        void dlg_Closed( IModalDialog sender , ModalDialogResult e ) {
            MessageBox.Show( e.ToString() );
        }
        
    }
}